package org.dromara.demo.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.demo.domain.vo.JsDocumentVo;
import org.dromara.demo.domain.bo.JsDocumentBo;
import org.dromara.demo.service.IJsDocumentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文档管理
 *
 * @author zhu
 * @date 2025-02-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/document")
public class JsDocumentController extends BaseController {

    private final IJsDocumentService jsDocumentService;

    /**
     * 查询文档管理列表
     */
    @SaCheckPermission("demo:document:list")
    @GetMapping("/list")
    public TableDataInfo<JsDocumentVo> list(JsDocumentBo bo, PageQuery pageQuery) {
        return jsDocumentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文档管理列表
     */
    @SaCheckPermission("demo:document:export")
    @Log(title = "文档管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JsDocumentBo bo, HttpServletResponse response) {
        List<JsDocumentVo> list = jsDocumentService.queryList(bo);
        ExcelUtil.exportExcel(list, "文档管理", JsDocumentVo.class, response);
    }

    /**
     * 获取文档管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("demo:document:query")
    @GetMapping("/{id}")
    public R<JsDocumentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jsDocumentService.queryById(id));
    }

    /**
     * 新增文档管理
     */
    @SaCheckPermission("demo:document:add")
    @Log(title = "文档管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JsDocumentBo bo) {
        return toAjax(jsDocumentService.insertByBo(bo));
    }

    /**
     * 修改文档管理
     */
    @SaCheckPermission("demo:document:edit")
    @Log(title = "文档管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JsDocumentBo bo) {
        return toAjax(jsDocumentService.updateByBo(bo));
    }

    /**
     * 删除文档管理
     *
     * @param ids 主键串
     */
    @Log(title = "文档管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jsDocumentService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 上传文件
     */

    @PostMapping("/upload")
    public R<JsDocumentVo> upload(@RequestParam("file") MultipartFile file) {
        return R.ok(jsDocumentService.upload(file));
    }

    /**
     * 下载文件
     */
    @GetMapping("/download/{id}")
    public void download(@NotNull(message = "主键不能为空") @PathVariable Long id, HttpServletResponse response) {
        JsDocumentVo jsDocumentVo = jsDocumentService.queryById(id);
        jsDocumentService.download(jsDocumentVo.getUrl(), response);
    }
}
